package com.cai.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cai.entity.PageResult;
import com.cai.entity.QueryPageBean;
import com.cai.entity.Result;
import com.cai.pojo.Address;
import com.cai.service.AddressService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class addressController {
    @Reference
    private AddressService addressService;
    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = addressService.pageQuery(queryPageBean);
        System.out.println(pageResult.getRows());
        return pageResult;
    }
    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody Address address) {
        try {
            System.out.println("1234");
            addressService.add(address);
            System.out.println(address);
            return new Result(true,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }
    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            addressService.delete(id);
            return new Result(true,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
        List<Address> list=addressService.findAll();
            System.out.println(list);
            return new Result(true,"查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }


}
